<!-- 部门管理页面视图 - 管理公司部门结构和信息 -->
<template>
  <div class="departments-content">
    <!-- 页面头部 -->
    <div class="page-header">
      <button class="back-btn" @click="goBack">
        <span class="back-icon">←</span>
        返回
      </button>
      <div class="header-info">
        <h1>部门管理</h1>
        <p v-if="selectedCompany">当前公司：{{ selectedCompany.name }}</p>
        <p v-else class="no-company">未选择公司</p>
      </div>
    </div>

    <!-- 部门管理内容 -->
    <main class="departments-main">
      <div v-if="!selectedCompany" class="no-company-warning">
        <div class="warning-icon">⚠️</div>
        <h3>请先选择要管理的公司</h3>
        <p>返回公司选择页面，选择一个公司后再进行部门管理</p>
        <button class="select-company-btn" @click="goToCompanySelect">
          选择公司
        </button>
      </div>
      
      <div v-else class="departments-container">
        <div class="department-actions">
          <button class="create-department-btn" @click="showCreateDepartmentModal = true">
            <span class="plus-icon">➕</span>
            创建部门
          </button>
        </div>

        <!-- 部门列表 -->
        <div class="department-list">
          <div v-if="loading" class="loading">加载中...</div>
          <div v-else-if="error" class="error-message">{{ error }}</div>
          <div v-else-if="departments.length === 0" class="empty-state">
            <div class="empty-icon">📋</div>
            <p>暂无部门数据</p>
            <p class="empty-tip">点击"创建部门"按钮添加第一个部门</p>
          </div>
          <div v-else class="department-items">
            <div 
              v-for="department in departments" 
              :key="department.id" 
              class="department-card"
            >
              <div class="department-header">
                <h3>{{ department.name }}</h3>
                <div class="department-actions-mini">
                  <button class="edit-btn">编辑</button>
                  <button class="delete-btn">删除</button>
                </div>
              </div>
              <div class="department-info">
                <p><strong>负责人：</strong>{{ department.manager || '未设置' }}</p>
                <p><strong>员工数：</strong>{{ department.employeeCount || 0 }}</p>
                <p><strong>描述：</strong>{{ department.description || '无' }}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>

    <!-- 创建部门模态框 -->
    <div v-if="showCreateDepartmentModal" class="modal-overlay" @click="showCreateDepartmentModal = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>创建部门</h3>
          <button class="close-btn" @click="showCreateDepartmentModal = false">×</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="handleCreateDepartment" class="department-form">
            <div class="form-group">
              <label for="department-name">部门名称 *</label>
              <input 
                type="text" 
                id="department-name" 
                v-model="departmentForm.name" 
                required 
                placeholder="请输入部门名称"
              />
            </div>
            <div class="form-group">
              <label for="department-manager">负责人</label>
              <input 
                type="text" 
                id="department-manager" 
                v-model="departmentForm.manager" 
                placeholder="请输入负责人姓名"
              />
            </div>
            <div class="form-group">
              <label for="department-description">部门描述</label>
              <textarea 
                id="department-description" 
                v-model="departmentForm.description" 
                placeholder="请输入部门描述"
                rows="3"
              ></textarea>
            </div>
            
            <div class="form-actions">
              <button type="button" @click="showCreateDepartmentModal = false">取消</button>
              <button type="submit" :disabled="!departmentForm.name">创建部门</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCompanyStore } from '../../store/company'
import type { Company } from '../../api/company'

// 路由实例
const router = useRouter()

// 获取公司状态管理实例
const companyStore = useCompanyStore()

// 响应式数据
const selectedCompany = ref<Company | null>(null)
const departments = ref<any[]>([])
const loading = ref(false)
const error = ref('')

// 创建部门模态框相关
const showCreateDepartmentModal = ref(false)
const departmentForm = ref({
  name: '',
  manager: '',
  description: ''
})

// 组件挂载时获取选中的公司
onMounted(() => {
  // 从状态管理中获取选中的公司
  selectedCompany.value = companyStore.getSelectedCompany()
  
  // 如果有选中的公司，加载部门数据
  if (selectedCompany.value) {
    loadDepartments()
  }
})

// 加载部门数据
const loadDepartments = async () => {
  loading.value = true
  error.value = ''
  try {
    // 这里应该调用API获取部门数据
    // 暂时使用模拟数据
    setTimeout(() => {
      departments.value = [
        {
          id: 1,
          name: '技术部',
          manager: '张三',
          employeeCount: 20,
          description: '负责公司产品的技术开发和维护'
        },
        {
          id: 2,
          name: '市场部',
          manager: '李四',
          employeeCount: 15,
          description: '负责市场推广和客户拓展'
        },
        {
          id: 3,
          name: '人事部',
          manager: '王五',
          employeeCount: 5,
          description: '负责人力资源管理'
        }
      ]
      loading.value = false
    }, 500)
  } catch (err) {
    console.error('获取部门列表失败:', err)
    error.value = '获取部门列表失败，请稍后重试'
    loading.value = false
  }
}

// 处理创建部门
const handleCreateDepartment = async () => {
  if (!departmentForm.value.name) {
    return
  }
  
  try {
    // 这里应该调用API创建部门
    // 暂时模拟创建成功
    const newDepartment = {
      id: Date.now(),
      name: departmentForm.value.name,
      manager: departmentForm.value.manager || '未设置',
      employeeCount: 0,
      description: departmentForm.value.description || '无'
    }
    
    departments.value.push(newDepartment)
    
    // 重置表单并关闭模态框
    departmentForm.value = {
      name: '',
      manager: '',
      description: ''
    }
    showCreateDepartmentModal.value = false
  } catch (err) {
    console.error('创建部门失败:', err)
    error.value = '创建部门失败，请稍后重试'
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 跳转到公司选择页面
const goToCompanySelect = () => {
  router.push('/company')
}
</script>

<style scoped>
.departments-content {
  height: 100%;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  overflow-y: auto;
  background-color: #f5f7fa;
}

/* 页面头部样式 */
.page-header {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 30px;
  background: white;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.back-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 8px 16px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.back-btn:hover {
  background: #5a67d8;
}

.back-icon {
  font-size: 16px;
}

.header-info h1 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 28px;
}

.header-info p {
  margin: 0;
  color: #666;
  font-size: 16px;
}

.no-company {
  color: #f56c6c;
}

/* 主要内容样式 */
.departments-main {
  max-width: 1200px;
  margin: 0 auto;
}

/* 未选择公司警告 */
.no-company-warning {
  text-align: center;
  background: white;
  padding: 60px 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.warning-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.no-company-warning h3 {
  color: #333;
  margin: 0 0 10px 0;
  font-size: 20px;
}

.no-company-warning p {
  color: #666;
  margin: 0 0 20px 0;
  font-size: 14px;
}

.select-company-btn {
  padding: 10px 24px;
  background-color: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.select-company-btn:hover {
  background-color: #5a67d8;
}

/* 部门容器 */
.departments-container {
  background: white;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 部门操作栏 */
.department-actions {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}

.create-department-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 10px 16px;
  background: #67c23a;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.create-department-btn:hover {
  background: #67c23a;
  opacity: 0.9;
}

.plus-icon {
  font-size: 16px;
}

/* 部门列表 */
.department-list {
  min-height: 200px;
}

.loading {
  text-align: center;
  padding: 40px;
  color: #606266;
}

.error-message {
  text-align: center;
  padding: 40px;
  color: #f56c6c;
  background-color: #fef0f0;
  border: 1px solid #fbc4c4;
  border-radius: 6px;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #909399;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.empty-state p {
  margin: 0 0 10px 0;
  font-size: 16px;
}

.empty-tip {
  font-size: 14px !important;
}

/* 部门卡片列表 */
.department-items {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.department-card {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 20px;
  transition: all 0.3s;
}

.department-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.department-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.department-header h3 {
  margin: 0;
  color: #333;
  font-size: 18px;
}

.department-actions-mini {
  display: flex;
  gap: 8px;
}

.edit-btn, .delete-btn {
  padding: 4px 8px;
  border: 1px solid #dcdfe6;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.3s;
}

.edit-btn {
  color: #667eea;
  border-color: #667eea;
}

.edit-btn:hover {
  background: #667eea;
  color: white;
}

.delete-btn {
  color: #f56c6c;
  border-color: #f56c6c;
}

.delete-btn:hover {
  background: #f56c6c;
  color: white;
}

.department-info p {
  margin: 8px 0;
  color: #666;
  font-size: 14px;
}

.department-info strong {
  color: #333;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #e4e7ed;
}

.modal-header h3 {
  margin: 0;
  color: #333;
  font-size: 20px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: #909399;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s;
}

.close-btn:hover {
  background-color: #f5f7fa;
  color: #606266;
}

.modal-body {
  padding: 20px;
}

/* 表单样式 */
.department-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-weight: 500;
  color: #606266;
  font-size: 14px;
}

.form-group input,
.form-group textarea {
  padding: 10px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.3s;
  font-family: inherit;
}

.form-group input:focus,
.form-group textarea:focus {
  outline: none;
  border-color: #667eea;
}

.form-group input::placeholder,
.form-group textarea::placeholder {
  color: #c0c4cc;
}

.form-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #e4e7ed;
}

.form-actions button {
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.form-actions button[type="button"] {
  background-color: #f5f7fa;
  color: #606266;
  border: 1px solid #dcdfe6;
}

.form-actions button[type="button"]:hover:not(:disabled) {
  background-color: #e6e8eb;
}

.form-actions button[type="submit"] {
  background-color: #667eea;
  color: white;
  border: none;
}

.form-actions button[type="submit"]:hover:not(:disabled) {
  background-color: #5a67d8;
}

.form-actions button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .departments-content {
    padding: 10px;
  }
  
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
  
  .header-info h1 {
    font-size: 24px;
  }
  
  .department-items {
    grid-template-columns: 1fr;
  }
  
  .form-actions {
    flex-direction: column;
  }
  
  .form-actions button {
    width: 100%;
  }
}
</style>